package edu.upc.fib.lamsandroid.client.adapters;

import android.content.Context;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import edu.upc.fib.lamsandroid.client.R;
import edu.upc.fib.lamsandroid.server.api.model.Activity;
import edu.upc.fib.lamsandroid.server.api.model.ToolType;

import java.util.List;

public class SequenceListAdapter extends ArrayAdapter<Activity> {

    private List<Activity> activities;
    private final Context context;

    private int textViewResourceId;

    private int selectedIndex;

    public SequenceListAdapter(Context context, int textViewResourceId, List<Activity> activities) {
        super(context, textViewResourceId, activities);
        this.context = context;
        this.activities = activities;

        this.textViewResourceId = textViewResourceId;
        selectedIndex = 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View rowView = inflater.inflate(R.layout.sequence_item, parent, false);

        if(activities != null) {
            final Activity activity = activities.get(position);

            TextView title = (TextView) rowView.findViewById(R.id.title);
            ImageView icon = (ImageView) rowView.findViewById(R.id.icon);

            if(activity.getTypeId().equals(Activity.TYPE_TOOL)) {
                switch (activity.getToolType().getExternalId().intValue()) {
                    case ToolType.FORUM:
                        icon.setImageResource(R.drawable.icon_forum);
                        break;
                    case ToolType.NOTICEBOARDX:
                        icon.setImageResource(R.drawable.icon_notebook);
                        break;
                    case ToolType.QUESTION_AND_ANSWER:
                        icon.setImageResource(R.drawable.icon_questionanswer);
                        break;
                    case ToolType.SUBMIT_FILE:
                        icon.setImageResource(R.drawable.icon_common_cartridge);
                        break;
                    case ToolType.CHAT:
                        icon.setImageResource(R.drawable.icon_chat);
                        break;
                    case ToolType.SHARED_RESOURCES:
                        icon.setImageResource(R.drawable.icon_rsrc);
                        break;
                    case ToolType.VOTING:
                        icon.setImageResource(R.drawable.icon_common_cartridge);
                        break;
                    case ToolType.NOTEBOOK:
                        icon.setImageResource(R.drawable.icon_notebook);
                        break;
                    case ToolType.SURVEY:
                        icon.setImageResource(R.drawable.icon_survey);
                        break;
                    case ToolType.SCRIBE:
                        icon.setImageResource(R.drawable.icon_common_cartridge);
                        break;
                    case ToolType.SHARED_TASKLIST:
                        icon.setImageResource(R.drawable.icon_task_list);
                        break;
                    case ToolType.GMAP:
                        icon.setImageResource(R.drawable.icon_gmap);
                        break;
                    case ToolType.SPREADSHEET:
                        icon.setImageResource(R.drawable.icon_spreadsheet);
                        break;
                    case ToolType.DATA_COLLECTION:
                        icon.setImageResource(R.drawable.icon_daco);
                        break;
                    case ToolType.WIKI:
                        icon.setImageResource(R.drawable.icon_wiki);
                        break;
                    case ToolType.IMAGE_GALLERY:
                        icon.setImageResource(R.drawable.icon_image_gallery);
                        break;
                    case ToolType.VIDEO_RECORDER:
                        icon.setImageResource(R.drawable.icon_common_cartridge);
                        break;
                    case ToolType.MINDMAP:
                        icon.setImageResource(R.drawable.icon_mindmap);
                        break;
                    case ToolType.ASSESSMENT:
                        icon.setImageResource(R.drawable.icon_assessment);
                        break;
                    case ToolType.PIXLR:
                        icon.setImageResource(R.drawable.icon_pixlr);
                        break;
                    case ToolType.EADVENTURE:
                        icon.setImageResource(R.drawable.icon_common_cartridge);
                        break;
                    case ToolType.WOOKIE:
                        icon.setImageResource(R.drawable.icon_wiki);
                        break;
                    case ToolType.SHARED_COMMON_CARTRIDGE:
                        icon.setImageResource(R.drawable.icon_common_cartridge);
                        break;
                    case ToolType.BBB:
                        icon.setImageResource(R.drawable.icon_bbb);
                        break;
                    case ToolType.MCQ:
                        icon.setImageResource(R.drawable.icon_mcq);
                        break;
                    case ToolType.DIMDIM:
                        icon.setImageResource(R.drawable.icon_common_cartridge);
                        break;
                }
            } else {
                icon.setImageResource(android.R.color.transparent);
            }

            title.setText(activity.getTitle());


            if(selectedIndex == position) {
                title.setTextSize(15);
                title.setTypeface(null, Typeface.BOLD);
                title.setPaintFlags(title.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);

            } else {
                title.setTextSize(12);
                title.setTypeface(null, Typeface.NORMAL);
                title.setPaintFlags(title.getPaintFlags() & (~Paint.UNDERLINE_TEXT_FLAG));
            }

            rowView.setSelected(selectedIndex == position);
        }

        return rowView;
/*
        View row = super.getView(position, convertView, parent);

        if(activities != null) {
            Activity activity = activities.get(position);
            TextView textView = (TextView) row;

            textView.setText(activity.getTitle());

            if(selectedIndex == position) {
                textView.setTextSize(15);
                textView.setTypeface(null, Typeface.BOLD);
                textView.setPaintFlags(textView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
            } else {
                textView.setTextSize(12);
                textView.setTypeface(null, Typeface.NORMAL);
                textView.setPaintFlags(textView.getPaintFlags() & (~Paint.UNDERLINE_TEXT_FLAG));
            }
        }

        return row;
*/
    }

    public void setSelectedIndex(int selectedIndex) {
        this.selectedIndex = selectedIndex;
        notifyDataSetChanged();
    }

}
